package com.wujian.love.study.designpattern;

import javax.swing.*;
import java.awt.*;

/**
 * @ClassName: SingletonDemo
 * @Description: 单例模式:某个类只能有一个实例，提供一个全局的访问点
 * @Author: wuj
 * @Date: 2021-01-08 11:18
 **/
public class SingletonDemo {

    public static void main(String[] args) {
        JFrame jf = new JFrame("饿汉单例模式测试");
        jf.setLayout(new GridLayout(1, 2));
        Container contentPane = jf.getContentPane();
        Bajie obj1 = Bajie.getInstance();
        contentPane.add(obj1);
        Bajie obj2 = Bajie.getInstance();
        contentPane.add(obj2);
        if (obj1 == obj2) {
            System.out.println("他们是同一人！");
        } else {
            System.out.println("他们不是同一人！");
        }

        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class Bajie extends JPanel {
    private static Bajie instance = new Bajie();
    private Bajie() {
        JLabel l1 = new JLabel(new ImageIcon("D:\\IDEA_SG\\love\\src\\main\\resources\\static\\img\\Bajie.jpg"));
        this.add(l1);
    }
    public static Bajie getInstance() {
        return instance;
    }
}



